package com.nosweattechnet.server;

import java.util.concurrent.ExecutionException;

import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;

import com.nosweattechnet.datatypes.Place;

//Gets a Place that matches a given id
public class PlaceIDAsyncTask extends CommunicatorAsyncTask {
	//Constants specified in php script
	private BasicNameValuePair tag=new BasicNameValuePair("tag","getPlaceById");
	private String placeIDString="place_uuid";
	
	//Initializes Communicator then adds tag, and then adds placeid with stored constant
	public PlaceIDAsyncTask(String placeid) {
		super();
		super.addNameValuePair(tag);
		super.addNameValuePair(new BasicNameValuePair(placeIDString,placeid));
	}
	
	//Parses the resulting JSONArray to a Place
	//IF NO JSONARRAY IS RETURNED, I.E. NO PLACE HAS THE SPECIFIED ID, THEN NULL WILL BE RETURNED
	public Place getPlace() throws InterruptedException, ExecutionException{
		JSONArray jarr=this.get();
		if(jarr.length()!=1){
			return null;
		}
		Place i=null;
		try {
			i = new Place(jarr.getJSONObject(0));
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return i;
	}
}
